package com.cloud.tools;

import com.cloud.myInterface.Factory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Tangtao
 * @Date: 2022/5/6 11:23
 * @Description: pool design pattern implementation classes
 * @ClassName: CommonResultFactory
 */

public class CommonResultFactory implements Factory {
    public static Map<String,CommonResult> successResultPool = new HashMap<>();
    public static Map<String,CommonResult> failureResultPool = new HashMap<>();


    @Override
    public <T> CommonResult<T> getResultInstance(int code, String msg, T data) {
        if(code == 200){
            CommonResult<T> result = successResultPool.get(msg);
            if(result!=null && result.getMsg().equals(msg)){
                if(data == result.getData())
                    return result;
            }
            CommonResult<T> commonResult = new CommonResult<>(200,msg,data);
            successResultPool.put(msg, commonResult);
            return commonResult;
        }else{
            CommonResult<T> result = failureResultPool.get(msg);
            if(result!=null && result.getMsg().equals(msg)){
                if(data == result.getData())
                    return result;
            }
            CommonResult<T> commonResult = new CommonResult<>(200,msg,data);
            failureResultPool.put(msg, commonResult);
            return commonResult;
        }
    }

}
